/*******************************************************************************
 * Copyright (c) 2009 Kevin Bierhoff and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Kevin Bierhoff - initial API and implementation
 *******************************************************************************/
package com.googlecode.twelfdevtools.twelfast;

public class ConstantDeclaration extends SourceLocation implements ITwelfDeclaration {
	
	public static ConstantDeclaration namedConst(
			Identifier c, 
			ITwelfTerm t,
			ITwelfTerm v, 
			int startRow, int startCol, int endRow, int endCol) {
		return new ConstantDeclaration(c, t, v, startRow, startCol, endRow, endCol);
	}
		
	public static ConstantDeclaration anonConst(
			ITwelfTerm t,
			ITwelfTerm v, 
			int startRow, int startCol, 
			int endRow, int endCol) {
		return new ConstantDeclaration(null, t, v, startRow, startCol, endRow, endCol);
	}
		
	private final Identifier cnst;
	private final ITwelfTerm term;
	private final ITwelfTerm value;

	private ConstantDeclaration(Identifier cnst, ITwelfTerm term, ITwelfTerm value, int startRow, int startCol, int endRow, int endCol) {
		super(startRow, startCol, endRow, endCol);
		this.cnst = cnst;
		this.term = term;
		this.value = value;
	}

	public Identifier getConst() {
		return cnst;
	}

	public ITwelfTerm getTerm() {
		return term;
	}
	
	public ITwelfTerm getValue() {
		return value;
	}

	public <T> T dispatch(ITwelfAstVisitor<T> visitor) {
		return visitor.constDecl(this);
	}

	@Override
	public String toString() {
		String name = (cnst == null ? "_" : cnst.toString());
		if(term == null) {
			return name + " = " + value;
		}
		else if(value == null) {
			return name + " : " + term + ".";
		}
		else {
			return name + " : " + term + "=" + value + ".";
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cnst == null) ? 0 : cnst.hashCode());
		result = prime * result + ((term == null) ? 0 : term.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ConstantDeclaration other = (ConstantDeclaration) obj;
		if (cnst == null) {
			if (other.cnst != null)
				return false;
		} else if (!cnst.equals(other.cnst))
			return false;
		if (term == null) {
			if (other.term != null)
				return false;
		} else if (!term.equals(other.term))
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}

}
